import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;

public class Guess {
    public static void main(String args[]) {
        WindowButton win = new WindowButton("猜数字");
    }
}

class WindowButton extends JFrame implements ActionListener {
    int number;
    JLabel hintLabel;
    private JLabel jTime = new JLabel();//计时器显示
    JTextField inputGuess;
    boolean o = true;
    JButton buttonGetNumber, buttonEnter;
    long time = 30;//计时秒数
    java.util.Timer timer = new Timer();

    WindowButton(String s) {
        super(s);
        addWindowListener(new WindowAdapter() { //匿名类的实例监视窗口事件
                              public void windowClosing(WindowEvent e) {
                                  dispose();
                              }
                          }
        );
        setLayout(new FlowLayout());
        buttonGetNumber = new JButton("得到一个随机数");
        add(buttonGetNumber);
        hintLabel = new JLabel("输入你的猜测：", JLabel.CENTER);
        hintLabel.setBackground(Color.red);
        inputGuess = new JTextField("0", 10);
        add(hintLabel);
        add(inputGuess);
        add(jTime);
        buttonEnter = new JButton("确定");
        add(buttonEnter);
        setBounds(100, 100, 150, 150);
        setVisible(true);
        buttonEnter.addActionListener(this);
        buttonGetNumber.addActionListener(this);

        validate();
    }

    class t implements Runnable {//新建线程计时

        public void run() {
            String str = "猜对了!";
            while (time >= 0 && !str.equals(hintLabel.getText())) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                time--;
                jTime.setText("还剩：" + time + "秒");
            }
        }
    }

    class tt extends TimerTask {//新建线程计时
        String str = "猜对了!";
        public void run() {
            while (time >= 0&& !str.equals(hintLabel.getText())) {
                try {
                   Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                time--;
                jTime.setText("还剩：" + time + "秒");
            }
        }
    }

    // @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == buttonGetNumber) {
            number = (int) (Math.random() * 100) + 1;
            hintLabel.setText("输入你的猜测：");
        } else if (e.getSource() == buttonEnter) {
            int guess = 0;
            tt tim=new tt();
            if (o == true) {
                timer.schedule(tim, 1000);//两种方法：1.新建计时器
                //new Thread(new t()).start();//2.开始线程
                o = false;
            }

            if (!jTime.getText().equals("还剩：0秒")) {
                try {
                    guess = Integer.parseInt(inputGuess.getText());
                    if (guess == number) {
                        hintLabel.setText("猜对了!");
                        JOptionPane.showMessageDialog(this, "猜对了！你赢了！", "消息对话框",
                                JOptionPane.WARNING_MESSAGE);//对话框
                        timer.cancel();
                        this.setVisible(false);//隐藏当前对话框
                        WindowButton win = new WindowButton("猜数字");
                    } else if (guess > number) {
                        hintLabel.setText("猜大了！");
                        inputGuess.setText(null);
                    } else if (guess < number) {
                        hintLabel.setText("猜小了！");
                        inputGuess.setText(null);
                    }
                } catch (NumberFormatException event) {
                    hintLabel.setText("请输入数字字符");
                }
            } else {
                JOptionPane.showMessageDialog(this, "超时！你输了！", "消息对话框",
                        JOptionPane.WARNING_MESSAGE);
                timer.cancel();
                this.setVisible(false);
                WindowButton win = new WindowButton("猜数字");
            }

        }
    }
}
